/**
 * Universita' degli Studi di Palermo, 2010-11, License GNU GPL 3 (http://www.gnu.org/licenses/gpl.html)
 */
package org.unipa.botplatform.model.simple;

import java.util.UUID;

import org.unipa.botplatform.model.Bot;
import org.unipa.botplatform.model.Planner;
import org.unipa.botplatform.model.Session;
import org.unipa.botplatform.model.simple.interaction.Crosstalk;
import org.unipa.botplatform.model.simple.interaction.Dialogue;

/**
 * @author Giuseppe Miceli
 *
 */
public class SimpleSession implements Session {

	/**  */
	private Bot bot;

	/**  */
	private Planner planner;

	/**  */
	private Dialogue dialogue;

	/**  */
	private String sessionId; 

	/**  */
	private String lastBotPhrase = null;

	/**
	 * 
	 */
	public void configure(Bot bot, Planner planner) {
		this.bot = bot;
		this.planner = planner;
		planner.attachBot(bot);
		this.dialogue = new Dialogue();
		String session;
		if ((session = bot.newSession()) != null) {
			this.sessionId = session;
		} else {
			this.sessionId = UUID.randomUUID().toString();		
		}
	}

	/**
	 * 
	 */
	public String interactWithBot(Object userPhrase) {

		String phrase = (String) userPhrase;
		Crosstalk crosstalk = new Crosstalk(lastBotPhrase, phrase);
		dialogue.addInteraction(crosstalk);
		planner.planThrough(sessionId, dialogue);

		lastBotPhrase = (String) bot.interactWithBot(this.sessionId, phrase);
		return lastBotPhrase;
	}

	public Dialogue getInteraction() {
		return this.dialogue;
	}

	public String start() {
		this.lastBotPhrase = (String)bot.startInteraction(sessionId);
		return this.lastBotPhrase;
	}

	public String getSessionId() {
		return this.sessionId.toString();
	}

	public String start(Object initialization) {
		this.lastBotPhrase = (String) bot.startInteraction(this.sessionId, initialization);
		return this.lastBotPhrase;
	}
}
